﻿#define IsDoubleData

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FPNN
{
    internal static class DataReader
    {
        private static string ReadLine(TextReader tr)
        {
            try
            {
                string result = tr.ReadLine();

                while (result.StartsWith("#"))
                {
                    result = ReadLine(tr);
                }

                return result;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static Fix16[][][] ReadConfiguration(string filePath)
        {
            TextReader tr = new StreamReader(filePath);

            Byte layerCount = Byte.Parse(ReadLine(tr));

            Fix16[][][] config = new Fix16[layerCount][][];

            for (byte i = 0; i < layerCount; i++)
            {
                UInt16[] matrixInfo = new UInt16[2];

                string[] resultS = ReadLine(tr).Split(';');
                matrixInfo[0] = UInt16.Parse(resultS[0].Trim());
                matrixInfo[1] = UInt16.Parse(resultS[1].Trim());

                Fix16[][] matrix = new Fix16[matrixInfo[0] + 2][];
                matrix[0] = new Fix16[2];
                matrix[0][0] = Fix16.Parse(resultS[2]);
                matrix[0][1] = Fix16.Parse(resultS[3]);

                for (byte j = 0; j < matrixInfo[0]; j++)
                {
                    matrix[j + 1] = StringAry2Double(ReadLine(tr).Split(';'));
                }

                matrix[matrix.Length - 1] = StringAry2Double(ReadLine(tr).Split(';'));
                

                config[i] = matrix;
            }

            tr.Close();
            return config;
        }

        public static Fix16[] ReadVector(string filePath)
        {
            TextReader tr = new StreamReader(filePath);
            string read = null;

            UInt16 fileInfo = UInt16.Parse(ReadLine(tr));

            Fix16[] vector = new Fix16[fileInfo];

            while ((read = ReadLine(tr)) != null)
            {
                string[] resultS = read.Split(';');
                for (int i = 0; i < fileInfo; i++)
                {
                    vector[i] = Fix16.Parse(resultS[i].Trim());
                }

            }
            tr.Close();
            return vector;
        }

        public static Fix16[][] ReadDataMarix(string filePath)
        {
            TextReader tr = new StreamReader(filePath);

            UInt16[] fileInfo = StringAry2UInt16(ReadLine(tr).Split(';'));
            Fix16[][] ary = new Fix16[fileInfo[0]][];

            for (int i = 0; i < fileInfo[0]; i++)                
            {
                string read = ReadLine(tr);
                string[] resultS = read.Split(';');
                Fix16[] result = StringAry2Double(resultS);
                ary[i] = result;
            }

            return ary;
        }

        private static UInt16[] StringAry2UInt16(string[] aryIn)
        {
            UInt16[] returnAry = new UInt16[aryIn.Length];

            for (int i = 0; i < aryIn.Length; i++)
            {
                returnAry[i] = UInt16.Parse(aryIn[i].Trim());
            }

            return (returnAry);
        }

        private static Fix16[] StringAry2Double(string[] aryIn)
        {
            Fix16[] returnAry = new Fix16[aryIn.Length];

            for (int i = 0; i < aryIn.Length; i++)
            {
                returnAry[i] = Fix16.Create(Double.Parse(aryIn[i].Trim()));
            }

            return (returnAry);
        }
    }
}